package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.vo.ScmCusInfoVo;
import org.jeecg.modules.scm.entity.ScmCloudChain;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmReceivables;
import org.jeecg.modules.scm.service.IScmCloudChainService;
import org.jeecg.modules.scm.service.IScmReceivablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 云链单API接口
 * @Author: zzq
 * @Date: 2021-11-04
 * @Version: V1.0
 */
@Api(tags = "云链单API接口")
@RestController
@RequestMapping("/scm/scmCloudChain")
@Slf4j
public class ScmCloudChainController extends JeecgController<ScmCloudChain, IScmCloudChainService> {
    @Autowired
    private IScmCloudChainService scmCloudChainService;
    @Autowired
    private IScmReceivablesService scmReceivablesService;
    @Resource
    private ISysBaseAPI sysBaseAPI;

    @ApiOperation(value = "通过应收账款ID查询云链单")
    @GetMapping(value = "/queryByReceivables")
    public Result<List<ScmCloudChain>> queryByReceivables(@RequestParam Integer receivablesId) {
        List<ScmCloudChain> scmCloudChainList = scmCloudChainService.queryByReceivables(receivablesId);
        if (oConvertUtils.listIsEmpty(scmCloudChainList)) {
            Result.error("云链单为空", null);
        }
        return Result.OK(scmCloudChainList);
    }

    @ApiOperation(value = "应收账款信息上链接口-测试")
    @GetMapping(value = "/onBlockchain")
    public Result<String> onBlockchain(Integer receivablesId) {
        String chainNo = scmCloudChainService.onBlockchain(receivablesId);
        if (StringUtils.isBlank(chainNo)) {
            Result.error("上链失败", null);
        }
        return Result.OK(chainNo);
    }

    /**
     * 查询当前登陆用户的所有云链单
     *
     * */
    @AutoLog(value = "查询当前登陆用户的所有云链单")
    @ApiOperation(value = "查询当前登陆用户的所有云链单", notes = "查询当前登陆用户的所有云链单")
    @GetMapping(value = "/getAllChain")
    public Result<?> getAllChain(ScmCloudChain scmCloudChain,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ScmCusInfoVo result = sysBaseAPI.findCusByUserId(user.getId()).getResult();
        scmCloudChain.setOwnerCusNo(result.getCusNo());
        QueryWrapper<ScmCloudChain> queryWrapper = QueryGenerator.initQueryWrapper(scmCloudChain, req.getParameterMap());
        Page<ScmCloudChain> page = new Page<ScmCloudChain>(pageNo, pageSize);
        IPage<ScmCloudChain> pageList = scmCloudChainService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 通过id查询融资情况
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询融资合同")
    @ApiOperation(value = "通过id查询融资合同", notes = "scm_cloud_chain-通过id查询融资合同")
    @GetMapping(value = "/queryFinaById")
    public Result<?> queryFinaById(@RequestParam(name = "id", required = true) String id) {
        ScmFinaContract scmFinaContract = scmCloudChainService.queryFinaContractById(id);
        if (scmFinaContract == null) {
            return Result.error("该云链单未融资");
        }
        return Result.OK(scmFinaContract);
    }

    /**
     * 通过id查询应收账款信息
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询应收账款信息")
    @ApiOperation(value = "通过id查询应收账款信息", notes = "通过id查询应收账款信息")
    @GetMapping(value = "/queryReceivablesById")
    public Result<?> queryReceivablesById(@RequestParam(name = "id", required = true) String id) {
        ScmCloudChain scmCloudChain = scmCloudChainService.getById(id);
        ScmReceivables scmReceivables = scmReceivablesService.getById(scmCloudChain.getOriginalChainNo());
        if (scmCloudChain == null) {
            return Result.error("未找到应收账款信息");
        }
        return Result.OK(scmReceivables);
    }

    /**
     * 分页列表查询
     *
     * @param scmCloudChain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "scm_cloud_chain-分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "scm_cloud_chain-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ScmCloudChain scmCloudChain,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ScmCloudChain> queryWrapper = QueryGenerator.initQueryWrapper(scmCloudChain, req.getParameterMap());
        Page<ScmCloudChain> page = new Page<ScmCloudChain>(pageNo, pageSize);
        IPage<ScmCloudChain> pageList = scmCloudChainService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param scmCloudChain
     * @return
     */
    @AutoLog(value = "scm_cloud_chain-添加")
    @ApiOperation(value = "scm_cloud_chain-添加", notes = "scm_cloud_chain-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ScmCloudChain scmCloudChain) {
        scmCloudChainService.save(scmCloudChain);
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param scmCloudChain
     * @return
     */
    @AutoLog(value = "scm_cloud_chain-编辑")
    @ApiOperation(value = "scm_cloud_chain-编辑", notes = "scm_cloud_chain-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ScmCloudChain scmCloudChain) {
        scmCloudChainService.updateById(scmCloudChain);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_cloud_chain-通过id删除")
    @ApiOperation(value = "通过id删除", notes = "scm_cloud_chain-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        scmCloudChainService.removeById(id);
        return Result.OK("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ScmCloudChain scmCloudChain = scmCloudChainService.getById(id);
        if (scmCloudChain == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(scmCloudChain);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param scmCloudChain
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmCloudChain scmCloudChain) {
        return super.exportXls(request, scmCloudChain, ScmCloudChain.class, "scm_cloud_chain");
    }


}
